package com.ubs.web.client.panel.login;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.ubs.dom.Account;
import com.ubs.web.client.AppContext;
import com.ubs.web.client.security.SecurityContext;

public class LoginInfoGrid extends Grid {
	
	private static final Label welcomeUserLabel = new Label("", false);

	private static final Label userBalanceLabel = new Label("", false);

	private static LoginInfoGrid loginInfoGrid;

	public static LoginInfoGrid getSingleton() {
		if (loginInfoGrid == null)
			loginInfoGrid = new LoginInfoGrid();
		
		return loginInfoGrid;
	}

	private LoginInfoGrid() {
		super(2, 3);
		
		updateUserInfo(null);
		welcomeUserLabel.setStyleName("login01");

		final Button logoutButton = new Button();
		logoutButton.setText(AppContext.getMessages().logoutButton());
		logoutButton.addStyleName("sendButton");
		
		userBalanceLabel.setStyleName("login01");
		
		setSize("200px", "73px");
		setWidget(0, 0, welcomeUserLabel);
		setWidget(0, 1, logoutButton);
		
		setWidget(1, 0, userBalanceLabel);
		
		logoutButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				SecurityContext.setAccount(null);
			}
		});
	}
	
	public void updateUserInfo(Account account) {
		String fistName = (account != null) ? account.getFirstName() : "";
		String lastName = (account != null) ? account.getLastName() : "";
		float userBalance = (account != null) ? account.getBalance() : 0;

		welcomeUserLabel.setText(AppContext.getMessages().welcomeUser(fistName, lastName));
		
		userBalanceLabel.setText(AppContext.getMessages().userBalance(userBalance));
	}

	
}
